package com.znk.aop;

import org.springframework.aop.framework.AopProxy;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MyJdkDynamicProxy implements AopProxy, InvocationHandler, Serializable {

    //目标对象(委托对象)
    private Object target = null;

    //接收目标对象，返回动态代理对象
    public Object bind(Object o) {
        target = o;
        return this.getProxy(MyJdkDynamicProxy.class.getClassLoader());
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        System.out.println(method.getName() + "的方法参数是：" + Arrays.asList(args));
        System.out.println(method.getName() + "的方法结果是：" + result);
        return result;
    }

    @Override
    public Object getProxy() {
        return null;
    }

    @Override
    public Object getProxy(ClassLoader classLoader) {
        return Proxy.newProxyInstance(classLoader, target.getClass().getInterfaces(), this);
    }
}
